<?php
    // Created By : Noan
    // B.S. Computer Science - De La Salle Canlubang
    // Dec 14, 2011

// Start - Checker for those users who will just go to the page by typing directly in the url.
if ($_POST) {
    // INCLUDES CONFIGURATION VARIABLES FOR DATABASE ACCESS
    include('../includes/siteConfig.php');

    // CONNECT TO DATABASE
    $connect = mysql_connect($hostName,$rootName,$dBasePassword) or die ('Unable to connect!');
    mysql_select_db($dBaseName) or die ('Unable to select database!');
    
    // GET VARIABLES VIA POST METHOD
    $docType = $_POST['docType'];
    
    if($docType=='Attachment'){
        $query = 
        "
            SELECT * 
            FROM DocumentSetup 
            WHERE type = '$docType'
        ";
    }
    else{
        $query = 
        "
            SELECT DS.documentSetupId documentSetupId, DS.type type , DS.documentName documentName
            FROM DocumentRoute DR,DocumentSetup DS
            WHERE DS.type = 'MainDocument' AND DS.documentSetupId = DR.documentSetupId
            Group By DS.documentSetupId
        ";
    }
    // QUERY - GETS ALL DOCUMENTS BASED ON DOCUMENT TYPE
    
    
    $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
    
    // SAVE QUERY RESULTS TO AN ARRAY > $row   
    while ($row = mysql_fetch_array($result)) {
        $jsondata = array();
        $jsondata['documentSetupId'] = $row['documentSetupId'];
        $jsondata['type'] = $row['type'];
        $jsondata['documentName'] = $row['documentName'];

        $feed[] = $jsondata;
    }

    echo json_encode($feed);
    
    mysql_free_result($result);
    mysql_close($connect);
}   // End - Checker for those users who will just go to the page by typing directly in the url.
else {
 echo "You are not authorized to view this page. This incident will be reported immediately.";
}
?>
